﻿using Common;
using DevExpress.XtraEditors;
using DLL;
using gregn6Lib;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Data;
using System.IO;
using System.Windows.Forms;

namespace NursingFormSystem
{
    public partial class BloodGlucPrints : XtraForm
    {
        private GridppReport Report = new GridppReport();
        private PatientInfoView PatientParams;
        private int[] IdArray;
        /// <summary>
        /// 血糖单打印
        /// </summary>
        /// <param name="idArray">要显示在打印报表中的数据的对应id数组</param>
        /// <param name="patParams">患者信息</param>
        public BloodGlucPrints(int[] idArray, PatientInfoView patParams)
        {
            InitializeComponent();
            this.IdArray = idArray;
            this.PatientParams = patParams;
            Report.LoadFromFile(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\血糖检测记录单.grf");
            Report.ConnectionString = Utility.GetDatabaseConnectionString();
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            axGRPrintViewer1.Report = Report;
        }
        private void ReportFetchRecord()
        {
            string select = @"SELECT '{0}' AS C1,'{1}' AS C2,'{2}' AS C3,'{3}' AS C4,'{4}' AS C5,'{5}' AS C6, [BloodsugarId] ,[PatientId] ,[VISIT_ID] ,[PAT_ID] ,[PatientName] ,[Age] ,[Bed_No] ,[InTime] ,[DepId] ,[DepName] ,[AreaId] ,[AreaName] ,[Date] ,[Time] ,[WeeHour] ,[breadfastbefore] ,[breadfastafter] ,[lunchbefore] ,[lunchafter] ,[dinnerbefore] ,[dinnerafter] ,[bedtime] ,[sugar1], case when sugartime1 = '00:00:00.0000000' then null else sugartime1 end as sugartime1 ,[sugar2] ,case when sugartime2 = '00:00:00.0000000' then null else sugartime2 end as sugartime2 ,[sugar3] ,case when sugartime3 = '00:00:00.0000000' then null else sugartime3 end as sugartime3 ,[sugar4] ,case when sugartime4 = '00:00:00.0000000' then null else sugartime4 end as sugartime4 ,[ketone1] ,case when ketonetime1 = '00:00:00.0000000' then null else ketonetime1 end as ketonetime1 ,[ketone2] ,case when ketonetime2 = '00:00:00.0000000' then null else ketonetime2 end as ketonetime2 ,[SubmituserId] ,[SubmituserName] ,[PrintTime] ,[syncSN] ,[blank1] ,[blank2] ,[blank3] FROM [BNNursingDocsSystem_Develop].[dbo].[t_Bloodsugar_Print](NOLOCK) WHERE ";
            select = string.Format(select, PatientParams.PATIENT_NAME, PatientParams.AGE, PatientParams.NURSE_CELL_NAME, PatientParams.BED_NO, PatientParams.IN_DATE, PatientParams.PATIENT_ID);
            string ids = string.Empty;
            for (int i = 0; i < this.IdArray.Length; i++)
            {
                ids += this.IdArray[i] + ",";
            }
            ids = ids.Trim(',');
            select = select + "BloodsugarId in (" + ids + ")   order by Date,Time ";
            DataSet ds = SqlHelper.ExecuteDataset(select, CommandType.Text);
            if (ds == null) return;
            if (ds.Tables.Count <= 0) return;
            Utility.FillRecordToReport(Report, ds.Tables[0]);
        }

        private void BloodGlucPrints_Load(object sender, EventArgs e)
        {
            axGRPrintViewer1.Start();
        }
    }
}
